classdef HelperFreqDiscriminator < matlab.System
    % atan2 频率鉴别器
    properties(Nontunable)
        % IntegTime
        type = 'atan2';

        FreqDiscriminatorToZero
        PhaseDiscriminatorToZero
    end

    methods(Access = public)
        function obj = HelperFreqDiscriminator(type, FreqDiscriminatorToZero, PhaseDiscriminatorToZero)  
            obj.type = type;
            obj.FreqDiscriminatorToZero = FreqDiscriminatorToZero;
            obj.PhaseDiscriminatorToZero = PhaseDiscriminatorToZero;
        end

        function pherr = PhaseDiscriminator(obj, integpval)
            if integpval == 0 || obj.PhaseDiscriminatorToZero
                % 若结果为零则相位误差设为0（排除无噪声零结果情况）
                pherr = 0;
            else
                pherr = atan(imag(integpval) / real(integpval));
            end
        end
    end

    methods(Access = protected)

        function fqyerr = stepImpl(obj, integ_coh, IntegTime)
%             fllin = sum(reshape(integ_coh, [], 2));
%             phasor = conj(fllin(1)) * fllin(2);
%             fqyerr = angle(phasor) / (pi * IntegTime);
%             patTransEst = ones(length(integ_coh), 1);
%             countIdxCBTransEst = [];
%             flagRealign = false;


            fqyerr = zeros(size(integ_coh, 1), 1);

            if obj.FreqDiscriminatorToZero
                return;
            end

            for i = 1:size(integ_coh, 1)
                fllin = sum(reshape(integ_coh(i, :), [], 2));
                phasor = conj(fllin(1)) * fllin(2);
                fqyerr(i) = angle(phasor) / (pi * IntegTime);
            end

            % patTransEst = ones(size(integ_coh.'));
            % countIdxCBTransEst = [];
            % flagRealign = false; % 若首次校正符号边界
        end

        function resetImpl(obj)
            % 重置频率鉴别器
        end
    end

end
